<?php

require_once ("includes/connect.php");
require_once ("includes/function.php");

$petition_id = $_REQUEST["petition_id"];
//echo $petition_id;

$db = new myDBS;
$Cur = new Currency;

$sql = "SELECT tbl_petition.*, expenses1_name, expenses2_name, expenses3_name FROM tbl_petition, expenses_lvl1, expenses_lvl2, expenses_lvl3 WHERE petition_id  = '$petition_id' and expenses_lvl1.expenses1_id = ex1_id and expenses_lvl2.expenses2_id = ex2_id and expenses_lvl3.expenses3_id = ex3_id";
//echo $sql;
$result = $db->runSQL($sql);
$rows = $db->fetch_object($result);

// just require TCPDF instead of FPDF
//define('FPDF_FONTPATH','fpdf/font/');
require_once('fpdf/TCPDF/tcpdf.php');
require_once('fpdf/FPDI/fpdi.php');

class PDF extends FPDI {

    /**
     * "Remembers" the template id of the imported page
     */
    var $_tplIdx;

    /**
     * include a background template for every page
     */
    function Header() {
        if (is_null($this->_tplIdx)) {
            $this->setSourceFile('form/petition1.pdf');
            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);

        /*
          //$this->AddFont('THSarabunNew','','THSarabunNew.php');
          //$this->SetFont('THSarabunNew', '', 9);
          $this->SetTextColor(255);
          $this->SetXY(60.5, 24.8);
          $this->Cell(0, 8.6, "TCPDF and FPDI");
         */
    }

    function Footer() {
        
    }

}

// initiate PDF
$pdf = new PDF();
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);

// add a page
$pdf->AddPage();

//$pdf->AddFont('THSarabunNew','','THSarabunNew.php');
//$pdf->SetFont("freeserif", "", 12);
$pdf->SetFont('thsarabunb', 'B', 15);
// now write some text above the imported page
$pdf->SetXY(26, 27);
$pdf->Write(0, $rows->lvl1_name);

$pdf->SetXY(28, 34);
$pdf->Write(0, $rows->budget_year);

$pdf->SetXY(90, 34);
$pdf->Write(0, $rows->p_id);

$pdf->SetXY(21, 41);
$pdf->Write(0, $rows->panngan2_name);

$pdf->SetXY(81, 41);
$pdf->Write(0, $rows->panngan3_name);

$pdf->SetXY(160, 48);
$pdf->Write(0, $rows->acc_id);

$pdf->SetXY(160, 55);
$pdf->Write(0, $rows->elass);

$pdf->SetXY(25, 48);
$pdf->Write(0, $rows->expenses1_name);

$pdf->SetXY(87, 48);
$pdf->Write(0, $rows->expenses2_name);

$pdf->SetXY(87, 55);
$pdf->Write(0, $rows->expenses3_name);

$pdf->SetXY(73, 69);
$pdf->Cell(41, 0, number_format($rows->inpValues, 2), 0, 0, 'R');

$pdf->SetXY(116, 69);
//$pdf->Write(0, $rows->comment, '', 0, 'J', true, 0, false, true, 0);
//$pdf->Cell(89,48, $rows->comment,0,	1,"L");
$pdf->MultiCell(90, 45, $rows->comment, 0, '', 0, 1, '', '', true);

$pdf->SetXY(73, 76);
$pdf->Cell(41, 0, number_format($rows->inpValues * $rows->inpTax / 100, 2), 0, 0, 'R');

$pdf->SetXY(73, 83);
$pdf->Cell(41, 0, number_format($rows->inpTotal, 2), 0, 0, 'R');

$pdf->SetXY(73, 90);
$pdf->Cell(41, 0, number_format($rows->inpValues * $rows->WithholdingTax / 100, 2), 0, 0, 'R');

$pdf->SetXY(73, 97);
$pdf->Cell(41, 0, number_format($rows->inpPenalty, 2), 0, 0, 'R');

$pdf->SetXY(73, 111);
$pdf->Cell(41, 0, number_format($rows->inpNet, 2), 0, 0, 'R');

$pdf->SetXY(53, 117.5);
$pdf->Write(0, $Cur->bahtThai($rows->inpNet));


$pdf->SetXY(20, 171);
$pdf->Cell(39, 0, number_format($rows->inpNet, 2), 0, 0, 'C');

$pdf->SetXY(88, 171);
$pdf->Cell(39, 0, number_format($rows->inpNet, 2), 0, 0, 'C');

$pdf->SetXY(155, 171);
$pdf->Cell(39, 0, number_format($rows->inpNet, 2), 0, 0, 'C');

$pdf->SetXY(29, 226);
$pdf->Cell(36, 0, number_format($rows->inpNet, 2), 0, 0, 'C');

$pdf->SetXY(70, 226);
$pdf->Write(0, $Cur->bahtThai($rows->inpNet));

$pdf->SetXY(27, 233);
$pdf->Write(0, $rows->company_name);

$pdf->SetXY(35, 247);
$pdf->Cell(55, 0, number_format($rows->inpNet, 2), 0, 0, 'C');



$pdf->Output('output_petition1.pdf', 'I');
?>